package canhcuadaihoc.tracnghiemonline;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Login_Dialog extends Dialog{

	public Login_Dialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public Login_Dialog(Context context,int theme) {
		super(context,theme);
		// TODO Auto-generated constructor stub
	}

	public static class Builder{
		private Context _context;
		private String _iconUri;
		private String _title;
		private String _message;
		private String _positiveBtnText;
		private String _negativeBtnText;
		private View _contentView;
		private DialogInterface.OnClickListener _positiveBtnClickListenter,_negativeBtnClickListener;
		
		public Builder(Context _context){
			this._context=_context;
		}
		
		//Set Icon
		public Builder setIconUri(String _icon)
		{
			this._iconUri=_icon;
			return this;
		}
		
		public Builder setIconUri(int _icon)
		{
			this._iconUri=(String)_context.getText(_icon);
			return this;
		}
		
		//Set Message
		public Builder setMesage(String _mess)
		{
			this._message=_mess;
			return this;
		}
		
		public Builder setMesage(int _mess)
		{
			this._message=(String)_context.getText(_mess);
			return this;
		}
		
		//Set Title
		public Builder setTitle(String _tit)
		{
			this._title=_tit;
			return this;
		}
		
		public Builder setTitle(int _tit)
		{
			this._title=(String)_context.getText(_tit);
			return this;
		}
		
		//Set ContentView
		
		public Builder setContentView(View _view)
		{
			this._contentView=_view;
			return this;
		}
		
		//Set PositiveButton
		public Builder setPositiveButton(int _positiveButtonText,DialogInterface.OnClickListener _listener)
		{
			this._positiveBtnText=(String)_context.getText(_positiveButtonText);
			this._positiveBtnClickListenter=_listener;
			return this;
		}
		
		public Builder setPositiveButton(String _positiveButtonText,DialogInterface.OnClickListener _listener)
		{
			this._positiveBtnText=_positiveButtonText;
			this._positiveBtnClickListenter=_listener;
			return this;
		}
		
		//Set NegativeButton
		public Builder setNegativeButton(int _negativeButtonText,DialogInterface.OnClickListener _listener)
		{
			this._negativeBtnText=(String)_context.getText(_negativeButtonText);
			this._negativeBtnClickListener=_listener;
			return this;
		}
		
		public Builder setNegativeButton(String _negativeButtonText,DialogInterface.OnClickListener _listener)
		{
			this._negativeBtnText=_negativeButtonText;
			this._negativeBtnClickListener=_listener;
			return this;
		}
		
		public Login_Dialog create(){
			LayoutInflater _inflater=(LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			final Login_Dialog _dialog=new Login_Dialog(_context,R.style.Login_Dialog);
			View _layout=_inflater.inflate(R.layout.dialog_login, null);
			_dialog.addContentView(_layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
			
			if(_iconUri!=null && _iconUri.length()>0){
				int imgRes=_context.getResources().getIdentifier(_iconUri, null, _context.getPackageName());
				Drawable _drawable=_context.getResources().getDrawable(imgRes);
				((ImageView)_layout.findViewById(R.id.img_logo_small)).setBackgroundDrawable(_drawable);
				
			}
			((TextView)_layout.findViewById(R.id.tw_notice_title)).setText(_title);
			
			if(_positiveBtnText!=null)
			{
			((Button)_layout.findViewById(R.id.bt_positive)).setText(_positiveBtnText);
			if(_positiveBtnClickListenter!=null)
			{
				((Button)_layout.findViewById(R.id.bt_positive)).setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						_positiveBtnClickListenter.onClick(_dialog, DialogInterface.BUTTON_POSITIVE);
					}
				});
			}
			}
			else{
				_layout.findViewById(R.id.bt_positive).setVisibility(View.GONE);
			}
			
			if(_negativeBtnText!=null)
			{
			((Button)_layout.findViewById(R.id.bt_negative)).setText(_negativeBtnText);
			if(_negativeBtnClickListener!=null)
			{
				((Button)_layout.findViewById(R.id.bt_negative)).setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						_negativeBtnClickListener.onClick(_dialog, DialogInterface.BUTTON_NEGATIVE);
					}
				});
			}
			}
			else{
				_layout.findViewById(R.id.bt_negative).setVisibility(View.GONE);
			}
			
			if(_message!=null)
			{
				((TextView)_layout.findViewById(R.id.tv_notice)).setText(_message);
				
			}
			else if(_contentView!=null){
				
				((LinearLayout)_layout.findViewById(R.id.ln_body)).removeAllViews();
				((LinearLayout)_layout.findViewById(R.id.ln_body)).addView(_contentView, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
				
			}
			_dialog.setContentView(_layout);
			return _dialog;
			
		}
	}
	
}
